package game;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class MjGame extends JFrame {
	
	List<Mj> mj = new ArrayList<Mj>();  //所有牌
	List<Mj> people1 = new ArrayList<Mj>();
	List<Mj> people2 = new ArrayList<Mj>();
	
	public MjGame(){
		super("麻将游戏");
//		JPanel panel=(JPanel) this.getContentPane();
//		panel.setBackground(Color.yellow);
		
		this.setSize(800,600);
		this.setResizable(false);
		this.setLocation(100, 100);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	// 初始化游戏  放108张牌
	void init() {
		mj.clear();
		for(int j=0;j<4;j++) {
			for(int k=1;k<=9;k++) {
				mj.add(new Mj(1,k));
				mj.add(new Mj(2,k));
				mj.add(new Mj(3,k));
			}
		}
		//洗牌
		Collections.shuffle(mj);
		//每人13张
		for(int j=0;j<13;j++) {
			people1.add( mj.remove(0));
			people2.add( mj.remove(0));
		}
		//排序
		Collections.sort(people1);
		Collections.sort(people2); 
	}
	
	//显示牌桌
	public void showDesk() {
		MjClicker mjclick= new MjClicker();

		for (int i = 0; i < people1.size(); i++) {
			//用户A
			Mj a= people1.get(i);
			a.setSize(39,60);
			a.setLocation(i*39+100, 470);
			a.x=i*39+100;
			a.y=470;
			a.showPic(true);
			a.addMouseListener(mjclick);
			this.add(a);
			
			//用户B
			Mj b= people2.get(i);
			b.setSize(39,60);
			b.setLocation(i*39+100, 50);
			b.x=i*39+100;
			b.y=50;
			b.showPic(false);
			this.add(b);
		}
		//刷新界面
		this.repaint();
	}
	
	//初始化牌的位置
	void initPosition(Mj p) {
		for (int i = 0; i < people1.size(); i++) {
			Mj a = people1.get(i);
			a.down();
		}
		p.up();
	}
	
	//监听器
	class MjClicker extends MouseAdapter{
		 @Override
		public void mouseClicked(MouseEvent e) {
			 Mj a =(Mj)e.getSource(); //触发事件的源对象
			 System.out.println("click me .."+a);
			 //	如果返回 true 表示更新位置		
			 a.up(); 
			 initPosition(a);
		} 
	}

}
